<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- CPU算力分配（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>现有两组服务器A和B&#xff0c;每组有多个算力不同的CPU&#xff0c;其中 A[i] 是 A 组第 i 个CPU的运算能力&#xff0c;B[i] 是 B组 第 i 个CPU的运算能力。</p> 
<p>一组服务器的总算力是各CPU的算力之和。</p> 
<p>为了让两组服务器的算力相等&#xff0c;允许从每组各选出一个CPU进行一次交换&#xff0c;</p> 
<p>求两组服务器中&#xff0c;用于交换的CPU的算力&#xff0c;并且要求从A组服务器中选出的CPU&#xff0c;算力尽可能小。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为L1和L2&#xff0c;以空格分隔&#xff0c;L1表示A组服务器中的CPU数量&#xff0c;L2表示B组服务器中的CPU数量。</p> 
<p>第二行输入为A组服务器中各个CPU的算力值&#xff0c;以空格分隔。</p> 
<p>第三行输入为B组服务器中各个CPU的算力值&#xff0c;以空格分隔。</p> 
<ul><li>1 ≤ L1 ≤ 10000</li><li>1 ≤ L2 ≤ 10000</li><li>1 ≤ A[i] ≤ 100000</li><li>1 ≤ B[i] ≤ 100000</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>对于每组测试数据&#xff0c;输出两个整数&#xff0c;以空格分隔&#xff0c;依次表示A组选出的CPU算力&#xff0c;B组选出的CPU算力。</p> 
<p>要求从A组选出的CPU的算力尽可能小。</p> 
<p></p> 
<h4>备注</h4> 
<ul><li>保证两组服务器的初始总算力不同。</li><li>答案肯定存在</li></ul> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2 2<br /> 1 1<br /> 2 2</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1 2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">从A组中选出算力为1的CPU&#xff0c;与B组中算力为2的进行交换&#xff0c;使两组服务器的算力都等于3。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2 2<br /> 1 2<br /> 2 3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1 2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1 2<br /> 2<br /> 1 3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2 3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3 2<br /> 1 2 5<br /> 2 4</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">5 4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>假设A组服务器算力之和为sumA&#xff0c;B组服务器算力之和为sumB&#xff0c;将A组的a和B组的b交换后&#xff0c;A组算力之和等于B组算力之和&#xff0c;则可得公式如下&#xff1a;</p> 
<blockquote> 
 <p>sumA - a &#43; b &#61; sumB - b &#43; a</p> 
 <p>sumA - sumB &#61; 2 * (a - b)</p> 
 <p>a - b &#61; (sumA - sumB) / 2</p> 
</blockquote> 
<p>其中 sumA, sumB 是已知的&#xff0c;因此&#xff0c;我们可以遍历A组所有元素a&#xff0c;计算出b&#61; a - (sumA - sumB)/2&#xff0c;看B组中是否存在对应b&#xff0c;若存在&#xff0c;则a b就是题解。</p> 
<hr /> 
<p>2023.12.05</p> 
<p>本题可能存在多组符合要求的用于交换的a、b&#xff0c;我们需要保存其中最小的a对应的a、b对</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" style="background-color:transparent;">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  // 可能有多组测试数据
  while (true) {
    try {
      const [l1, l2] &#61; (await readline()).split(&#34; &#34;).map(Number);

      const A &#61; (await readline()).split(&#34; &#34;).map(Number);
      const B &#61; (await readline()).split(&#34; &#34;).map(Number);

      let sumA &#61; 0;
      for (let a of A) {
        sumA &#43;&#61; a;
      }

      let sumB &#61; 0;
      const setB &#61; new Set();

      for (let b of B) {
        sumB &#43;&#61; b;
        setB.add(b);
      }

      // 由于本题必然存在解&#xff0c;因此sumA-sumB的结果肯定可以整除2&#xff0c;如果不能整除则half_diff为小数&#xff0c;
      // 而half_diff &#61; a - b&#xff0c;其中a,b都是整数&#xff0c;因此不可能存在half_diff是小数的情况
      const half_diff &#61; (sumA - sumB) / 2;

      // 记录用于交换的最小的a
      let minA &#61; Infinity;
      // 记录题解
      let ans &#61; &#34;&#34;;

      for (let a of A) {
        const b &#61; a - half_diff;

        if (setB.has(b)) {
          if (a &lt; minA) {
            minA &#61; a;
            ans &#61; &#96;${a} ${b}&#96;;
          }
        }
      }

      console.log(ans);
    } catch (e) {
      break;
    }
  }
})();
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    // 可能有多组测试数据
    while (sc.hasNext()) {
      int l1 &#61; sc.nextInt();
      int l2 &#61; sc.nextInt();

      int sumA &#61; 0;
      int[] A &#61; new int[l1];
      for (int i &#61; 0; i &lt; l1; i&#43;&#43;) {
        int a &#61; sc.nextInt();
        sumA &#43;&#61; a;
        A[i] &#61; a;
      }

      int sumB &#61; 0;
      HashSet&lt;Integer&gt; setB &#61; new HashSet&lt;&gt;();
      for (int i &#61; 0; i &lt; l2; i&#43;&#43;) {
        int b &#61; sc.nextInt();
        sumB &#43;&#61; b;
        setB.add(b);
      }

      // 由于本题必然存在解&#xff0c;因此sumA-sumB的结果肯定可以整除2&#xff0c;如果不能整除则half_diff为小数&#xff0c;
      // 而half_diff &#61; a - b&#xff0c;其中a,b都是整数&#xff0c;因此不可能存在half_diff是小数的情况
      int half_diff &#61; (sumA - sumB) / 2;

      // 记录用于交换的最小的a
      int minA &#61; Integer.MAX_VALUE;
      String ans &#61; &#34;&#34;;

      for (int a : A) {
        int b &#61; a - half_diff;

        if (setB.contains(b)) {
          if (a &lt; minA) {
            minA &#61; a;
            ans &#61; a &#43; &#34; &#34; &#43; b;
          }
        }
      }

      System.out.println(ans);
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python">import sys

while True:
    try:
        l1, l2 &#61; map(int, input().split())
        A &#61; list(map(int, input().split()))
        B &#61; list(map(int, input().split()))
    
        sumA &#61; sum(A)
    
        sumB &#61; 0
        setB &#61; set()
    
        for b in B:
            sumB &#43;&#61; b
            setB.add(b)
    
        # 由于本题必然存在解&#xff0c;因此sumA-sumB的结果肯定可以整除2&#xff0c;如果不能整除则half_diff为小数&#xff0c;
        # 而half_diff &#61; a - b&#xff0c;其中a,b都是整数&#xff0c;因此不可能存在half_diff是小数的情况
        half_diff &#61; (sumA - sumB) // 2
    
        # 记录用于交换的最小的a
        minA &#61; sys.maxsize
        # 记录题解
        ans &#61; &#34;&#34;
    
        for a in A:
            b &#61; a - half_diff
    
            if b in setB:
                if a &lt; minA:
                    minA &#61; a
                    ans &#61; f&#34;{a} {b}&#34;
    
        print(ans)
    except:
        break

</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;limits.h&gt;
#include &lt;string.h&gt;

#define MAX_SIZE 10000
#define MAX_VALUE 100000

int main() {
    // 可能有多组测试数据
    while(1) {
        int l1, l2;
        int res &#61; scanf(&#34;%d %d&#34;, &amp;l1, &amp;l2);
        if(res !&#61; 2) {
            break;
        }

        int A[MAX_SIZE];
        int A_size &#61; 0;

        int sumA &#61; 0;

        while (scanf(&#34;%d&#34;, &amp;A[A_size])) {
            sumA &#43;&#61; A[A_size];
            A_size&#43;&#43;;
            if(getchar() !&#61; &#39; &#39;) break;
        }

        int B[MAX_SIZE];
        int B_size &#61; 0;

        int sumB &#61; 0;
        int setB[MAX_VALUE &#43; 1] &#61; {0};

        while (scanf(&#34;%d&#34;, &amp;B[B_size])) {
            sumB &#43;&#61; B[B_size];
            setB[B[B_size]] &#61; 1;
            B_size&#43;&#43;;
            if(getchar() !&#61; &#39; &#39;) break;
        }

        // 由于本题必然存在解&#xff0c;因此sumA-sumB的结果肯定可以整除2&#xff0c;如果不能整除则half_diff为小数&#xff0c;
        // 而half_diff &#61; a - b&#xff0c;其中a,b都是整数&#xff0c;因此不可能存在half_diff是小数的情况
        int half_diff &#61; (sumA - sumB) / 2;

        int minA &#61; INT_MAX;
        char ans[15];

        for(int i&#61;0; i&lt;A_size; i&#43;&#43;) {
            int a &#61; A[i];
            int b &#61; a - half_diff;
            if(b &gt; 0 &amp;&amp; setB[b] &amp;&amp; a &lt; minA) {
                minA &#61; a;
                char tmp[15];
                sprintf(tmp, &#34;%d %d&#34;, a, b);
                strcpy(ans, tmp);
            }
        }

        puts(ans);
    }

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>